// pages/user/edit/edit.js
var http = require('../../../../utils/http.js');
var formUtil = require('../../../../utils/formUtil.js');
Page({

  onGotUserInfo: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },
  //修改用户信息
  editUser() {
    if (this.data.char === "phone") {
      if (!formUtil.checkMobile(this.data.editValue)){
        wx.showToast({
          title: '手机号码格式错误',
          icon: 'none'
        })
        return false;
      } 
    }
    let that = this;
    let obj = {};
    obj[this.data.char] = this.data.editValue;
    obj = Object.assign({}, this.data.user, obj)
    http.POST({
      url: "api/user/userInfoSave",
      data: obj,
      success: function(res) {
        console.log(res);
        if (res.data.error === 0) {
          wx.switchTab({
            url: '../../index/index',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  //检测用户输入信息
  watchInput(event) {
    let that = this;
    let value = event.detail.value;
    let property = "user." + that.data.char;
     
    this.setData({
      editValue: value
    })
  },
  //检测用户输入验证码
  watchInputCode(event) {
    let that = this;
    let value = event.detail.value;
    this.setData({
      "user.code": value
    })
  },
  //点击获取手机验证码
  getCode() {
    if (!formUtil.checkMobile(this.data.editValue)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false;
    }
    let time = this.data.time;
    let that = this;
    if(time===60){
      that.sendCode();
      that.data.timer = setInterval(function () {
        if (time === 0) {
          clearInterval(that.data.timer);
          that.setData({
            "tips": "获取手机验证码"
          })
          that.setData({
            "time": 60
          })
        } else {
          that.setData({
            "time": time--
          })

          that.setData({
            "tips": time + "s后重新获取"
          })
        }
      }, 1000)
    }
  },
  //发送短信验证码
  sendCode(){
    let that = this;
    http.POST({
      url: "api/publics/sendSms",
      data: {
        mobile: this.data.editValue,
        template_id: "SMS_134180180"     
      },
      success: function(res){
        console.log(res);
        if (res.data.error === 0) {
          
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    tips: "获取手机验证码",
    timer: '',
    time: 60,
    title: "",
    char: "",
    editValue: "",
    user: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    wx.setNavigationBarTitle({
      title: "修改" + options.title
    });
    this.setData({
      "title": options.title
    });
    this.setData({
      "char": options.char
    });
    this.setData({
      "user": wx.getStorageSync('user')
    });
    let that = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
            }
          })
        } else {
          that.onGotUserInfo();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})